package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.Order;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/order")
@AllArgsConstructor
@ResponseResult
public class OrderController {

    private final OrderService orderService;

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String create_time = req.getParameter("create_time");
        String receiving_address = req.getParameter("receiving_address");
        String user_id = req.getParameter("user_id");

        Boolean save = orderService.save(new Order(id, LocalDateTime.now(), receiving_address,user_id));
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String create_time = req.getParameter("create_time");
        String receiving_address = req.getParameter("receiving_address");
        String user_id = req.getParameter("user_id");

        Boolean update = orderService.update(new Order(id, LocalDateTime.now(), receiving_address, user_id));
        return ResponseVo.buildSuccess(update);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        boolean rs = orderService.remove(id);
        return ResponseVo.buildSuccess(rs);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Order order = orderService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(order);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) {
        List<Order> orders = orderService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(orders);
    }

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<Order> orderByPage = orderService.findOrderByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(orderByPage);
    }
}
